<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>component tag</title>
  <script src="../assets/js/vue.js"></script>
</head>
<body>
  <h1>component tag</h1>
  <hr>
  <div id="app">
    <component :is="who"></component>
    <button @click="changeComponent">changeComponent</button>
  </div>
  <script>
    var componentA = {
      template: `<div style="color: red;">i'm componetA</div>`
    }
    var componentB = {
      template: `<div style="color: green;">i'm componetB</div>`
    }
    var componentC = {
      template: `<div style="color: pink;">i'm componetC</div>`
    }
    var vue = new Vue({
      el: '#app',
      data: {
        // 这里如果who的值用‘字符串’表示，则必须在components选项中注册，如果直接用‘变量’表示则可以不用在components选项中注册也可以
        who: 'componentA'
      },
      components: {
        componentA: componentA,
        componentB: componentB,
        componentC: componentC
      },
      methods: {
        changeComponent: function () { 
          if (this.who === 'componentA') {
            this.who = 'componentB';
          } else if (this.who === 'componentB') {
            this.who = 'componentC';
          } else {
            this.who = 'componentA';
          }
        }
      }
    })
  </script>
</body>
</html>